import { mediaTypeList } from './consts';

// 处理数据千分位分隔符
export function numberWithCommas(n: number | string, empyt = '0') {
    if (n === 0 || n === '0') {
        return '0';
    } else if (n === null || n === undefined) {
        return empyt;
    }
    return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');

}

// 处理价格千分位，且保留两位小数，不足两位补00
export const formatNumber = (num: number | string): string => {
    let myNum = num;
    if (typeof myNum === 'string') {
        myNum = parseFloat(myNum);
    }
    if (isNaN(myNum)) {
        return '0';
    }
    const [integer, fraction = ''] = myNum.toFixed(2).split('.');
    const formattedInteger = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    const formattedFraction = fraction.padEnd(2, '0');
    return `${formattedInteger}.${formattedFraction}`;
};

// 根据媒体类型获取媒体类型名称
export function getMediaNameByType(type: number) {
    const name = mediaTypeList.find(item => item.type === type)?.name || '';
    return name;
}

export const getMediaName = (mediaTypes: number[], pointSelectionMode: number) => {
    const isOnlyOne = mediaTypes.every(item => item === 1);
    const isOnlyTwo = mediaTypes.every(item => item === 2);
    const isOneAndTwo = mediaTypes.includes(1) && mediaTypes.includes(2);
    if (isOnlyOne && pointSelectionMode === 1) {
        return '全城引爆-智能屏';
    }
    if (isOnlyOne && pointSelectionMode === 2) {
        return '半城引爆-智能屏';
    }
    if (isOneAndTwo && pointSelectionMode === 1) {
        return '全部智能屏+全部LCD引爆';
    }
    if (isOnlyTwo) {
        return '全部LCD引爆';
    }
    return '';
};

export const getMediaDesc = (mediaTypes: number[], pointSelectionMode: number) => {
    const isOnlyOne = mediaTypes.every(item => item === 1);
    const isOnlyTwo = mediaTypes.every(item => item === 2);
    const isOneAndTwo = mediaTypes.includes(1) && mediaTypes.includes(2);
    if (isOnlyOne && pointSelectionMode === 1) {
        return '策略说明：选择全部智能屏引爆';
    }
    if (isOnlyOne && pointSelectionMode === 2) {
        return '策略说明：每个楼盘选择一半的智能屏引爆';
    }
    if (isOneAndTwo && pointSelectionMode === 1) {
        return '策略说明：选择全城的智能屏与LCD引爆';
    }
    if (isOnlyTwo) {
        return '策略说明：选择全部LCD引爆';
    }
};
